package Base.多线程同步.同步方法;

/**
 * @author 陈鹏
 */
public class Example13 {
    public static void main(String[] args) {
        SaleThread3 saleThread3 = new SaleThread3();
        new Thread(saleThread3, "thread1").start();
        new Thread(saleThread3, "thread2").start();
        new Thread(saleThread3, "thread3").start();
        new Thread(saleThread3, "thread4").start();

    }
}

class SaleThread3 implements Runnable {
    private int tickets = 100;
    @Override
    public void run() {
        while (true) {
            sale();
        }
    }
    private synchronized void sale() {
        if (tickets > 0) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "出售的票为第" + tickets-- + "张");
        }
    }
}
